package xin.printk.android.module.theme;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.View;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import xin.printk.android.BasicActivity;
import xin.printk.android.R;
import xin.printk.android.data.entity.Theme;
import xin.printk.android.utils.AppConstants;
import xin.printk.android.utils.PrefUtils;
import xin.printk.android.view.adapter.AbstractRecyclerAdapter;
import xin.printk.android.view.adapter.ThemeGridAdapter;


public class ThemeActivity extends BasicActivity {

    private Toolbar mToolbar;
    private ConstraintLayout mCurrentThemeLayout;
    private TextView mCurrentThemeText;
    private List<Theme> mThemes;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_theme);
        mToolbar = findViewById(R.id.theme_toolbar);
        setToolbar(mToolbar);
        setCanFinish(true);

        mCurrentThemeLayout = findViewById(R.id.current_theme_layout);
        mCurrentThemeText = findViewById(R.id.tv_theme_description);
        mCurrentThemeLayout.setBackgroundColor(getThemeColor());
        mCurrentThemeText.setText(getThemeName());

        RecyclerView mThemeRecyclerView = findViewById(R.id.rv_theme_list);
        mThemes = mockData();
        ThemeGridAdapter mThemeAdapter = new ThemeGridAdapter(this, mThemes);
        mThemeRecyclerView.setLayoutManager(new GridLayoutManager(this, 4));
        mThemeRecyclerView.setAdapter(mThemeAdapter);

        ItemTouchHelper.Callback callback = new ThemeGridAdapter.ItemDragCallback(mThemeAdapter);
        ItemTouchHelper helper = new ItemTouchHelper(callback);
        helper.attachToRecyclerView(mThemeRecyclerView);

        mThemeAdapter.setOnItemClickListener(new AbstractRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onClick(View view, int position) {
                PrefUtils.putInt(AppConstants.THEME_COLOR, mThemes.get(position).getColor());
                PrefUtils.putString(AppConstants.THEME_COLOR_NAME, mThemes.get(position).getDescription());
                mToolbar.setBackgroundColor(mThemes.get(position).getColor());
                mCurrentThemeLayout.setBackgroundColor(mThemes.get(position).getColor());
                mCurrentThemeText.setText(mThemes.get(position).getDescription());
            }
        });
    }

    private List<Theme> mockData() {
        List<Theme> themes = new ArrayList<>();

        Theme blue = new Theme(getResources().getColor(R.color.blue_light), getString(R.string.official_blue));
        themes.add(blue);

        Theme green = new Theme(getResources().getColor(R.color.green_light), getString(R.string.official_green));
        themes.add(green);

        Theme red = new Theme(getResources().getColor(R.color.red_light), getString(R.string.official_red));
        themes.add(red);

        Theme cyan = new Theme(getResources().getColor(R.color.cyan_light), getString(R.string.official_cyan));
        themes.add(cyan);

        Theme yellow = new Theme(getResources().getColor(R.color.yellow_light), getString(R.string.official_yellow));
        themes.add(yellow);

        Theme orange = new Theme(getResources().getColor(R.color.orange_light), getString(R.string.official_orange));
        themes.add(orange);

        Theme purple = new Theme(getResources().getColor(R.color.purple_light), getString(R.string.official_purple));
        themes.add(purple);

        Theme black = new Theme(getResources().getColor(R.color.black_light), getString(R.string.official_black));
        themes.add(black);

        Theme gray = new Theme(getResources().getColor(R.color.gray_light), getString(R.string.official_gray));
        themes.add(gray);

        return themes;
    }
}
